<template>
  <div>
     <el-card>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="广告名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="广告位置">
          <el-select v-model="form.spaceId" placeholder="请选择广告位置">
            <el-option v-for="item in spaces" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="广告图片">
          <course-img v-model="form.img"></course-img>
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio-group v-model="form.status">
          <el-radio :label='1'>上线</el-radio>
          <el-radio :label='0'>下线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告链接">
          <el-input v-model="form.link"></el-input>
        </el-form-item>
        <el-form-item label="广告备注">
          <el-input type="textarea" v-model="form.htmlContent"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="submit">提交</el-button>
        </el-form-item>
      </el-form>
     </el-card>
  </div>
</template>

<script>
import CourseImg from '@/views/course/components/courseImg.vue'
import { getAllSpaces, saveOrUpdate, getAdById } from '@/services/advert'

export default {
  components: {
    CourseImg
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    advId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      spaces: [],
      form: {
        name: '',
        spaceId: 0,
        htmlContent: '',
        text: '',
        img: '',
        link: '',
        startTime: '',
        endTime: '',
        status: 0,
        priority: 0
      }
    }
  },
  methods: {
    async submit () {
      console.log()
      const { data } = await saveOrUpdate(this.form)
      console.log(data)
      if (data.state === 1) {
        this.$message.success(this.isEdit ? '保存成功' : '添加成功')
        this.$router.push('/advert')
      }
    },
    async loadData () {
      const { data } = await getAllSpaces()
      // console.log(data)
      if (data.state === 1) {
        this.spaces = data.content
      }
    },
    async loadEditData () {
      const { data } = await getAdById(this.advId)
      console.log(data)
      this.form = data.content
    }
  },
  created () {
    this.loadData()
    console.log(this.isEdit)
    if (this.isEdit) {
      this.loadEditData()
    }
  }
}
</script>

<style>

</style>
